package com.example.server.controller;

import com.example.server.entity.History;
import com.example.server.entity.Product;
import com.example.server.redis.RedisService;
import com.example.server.service.HistoryService;
import com.example.server.service.ProductService;
import com.example.server.util.Util;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("history")
public class HistoryController extends BaseController{

    private final HistoryService historyService;
    private final ProductService productService;
    private final RedisService redisService;

    public HistoryController(HistoryService historyService, ProductService productService, RedisService redisService) {
        this.historyService = historyService;
        this.productService = productService;
        this.redisService = redisService;
    }


//    public HistoryController(HistoryService historyService, ProductService productService) {
//        this.historyService = historyService;
//        this.productService = productService;
//    }

    /**
     *
     * @return
     */
    @PostMapping("create")
    private int create(){
        History history = new History();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));
        history.setUid(uid);
        history.setPid(pid);
        int num = historyService.queryNum("queryNum",history);
        if(num > 0) {
            Date date = new Date();
            history.setTime(date);
            return historyService.modify(history);
        }else {
            System.out.println(num);
            return historyService.create(history);
        }
    }

    @GetMapping("queryByUid")
    private List<History> queryByUid(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        return historyService.queryList("queryByUid",uid);
    }

//    @DeleteMapping("removeById")
//    private int removeById(@RequestBody Map<String,Integer> map){
//        int id = map.get("id");
//        return historyService.removeById(id);
//    }
    @DeleteMapping("removeById/{id}")
    private int removeById(@PathVariable int id){
        return historyService.removeById(id);
    }

}
